/**
 * 部门管理初始化
 */
var ReservationsAggregate = {
    id: "ReservationsAggregateTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ReservationsAggregate.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '预约号', field: 'reservationsNum', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,width:120},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                }else{ return "未知"}
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '微信号', field: 'wechatNumber', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true,width:120},
        {
            title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '门诊', field: 'arrivalStore', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: 'TC', field: 'tcName', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '成交金额', field: 'money', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '成交时间', field: 'getTime', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '首次到店', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '预约病种', field: 'appointmentPayService', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '访问病种', field: 'accessDisease', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '成交项目', field: 'department', align: 'center', valign: 'middle', sortable: true,width:120},
    ]
    return columns;
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
/**
 * 检查是否选中
 */
ReservationsAggregate.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ReservationsAggregate.seItem = selected[0];
        return true;
    }
};


/**
 * 查询预约客户列表
 */
ReservationsAggregate.search = function () {
   var endTime= document.getElementById("endTime").value;
   var startTime=document.getElementById("startTime").value;
   var time=endTime-startTime;
   if(startTime==null||startTime==""){
       alert("请选择成交开始时间");
       return;
   }
    if(startTime==null||startTime==""){
        alert("请选择成交结束时间");
        return;
    }
    ReservationsAggregate.table.refresh({query: ReservationsAggregate.formParams()});
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/reservationsAggregate/sumBorrowAmount',
        data: ReservationsAggregate.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html(data.borrowAmount_sum);
        }
    });
};

ReservationsAggregate.resetSearch = function () {
    $("#customerName").val("");
    $("#customerPhone").val("");
    $("#archiving").val("");
    $("#department").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#startArrivalTime").val("");
    $("#endArrivalTime").val("");
    $("#arrivalStore").val("");
    $("#reservationsNum").val("");
    $("#oneChannel").val("");
    $("#twoChannel").val("");
    $("#thrChannel").val("");
    $("#wechatNumber").val("");
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    Reservations.search();
};

ReservationsAggregate.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '添加建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_archiving'
    });
    this.layerIndex = index;
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ReservationsAggregate.formParams = function () {
    var queryData = {};
    queryData['customerName'] = $("#customerName").val();
    queryData['customerPhone'] = $("#customerPhone").val();
    queryData['department'] = $("#department").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startArrivalTime'] = $("#startArrivalTime").val();
    queryData['endArrivalTime'] = $("#endArrivalTime").val();
    queryData['archiving'] = $("#archiving").val();
    queryData['arrivalStore'] = $("#arrivalStore").val();
    queryData['oneChannel'] = $("#oneChannel").val();
    queryData['twoChannel'] = $("#twoChannel").val();
    queryData['thrChannel'] = $("#thrChannel").val();
    queryData['reservationsNum'] = $("#reservationsNum").val();
    queryData['wechatNumber'] = $("#wechatNumber").val();
    return queryData;
}

ReservationsAggregate.reservationsExport = function () {
    var queryData = {};
    queryData['customerName'] = $("#customerName").val();
    queryData['customerPhone'] = $("#customerPhone").val();
    queryData['department'] = $("#department").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startArrivalTime'] = $("#startArrivalTime").val();
    queryData['endArrivalTime'] = $("#endArrivalTime").val();
    queryData['archiving'] = $("#archiving").val();
    queryData['arrivalStore'] = $("#arrivalStore").val();
    queryData['oneChannel'] = $("#oneChannel").val();
    queryData['twoChannel'] = $("#twoChannel").val();
    queryData['thrChannel'] = $("#thrChannel").val();
    queryData['reservationsNum'] = $("#reservationsNum").val();
    queryData['wechatNumber'] = $("#wechatNumber").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/reservationsAggregate/reservationsExport?' + param;
}

$(function () {
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = ReservationsAggregate.initColumn();
    var table = new BSTable(ReservationsAggregate.id, "/reservationsAggregate/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ReservationsAggregate.formParams());
    ReservationsAggregate.table = table.init();
    ReservationsAggregate.search();
    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/payService/getList", function (data) {
        var txt = '<option value="">--请选择预约项目--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].department + '">' + data[i].department + '</option>';
        }
        document.getElementById('department').innerHTML = txt;
        //状态单选
        var seletepayservicename = $('#payservicenameValue').val();
        if (seletepayservicename != "") {
            $("#department option[value=" + seletepayservicename + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('arrivalStore').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName != "") {
            $("#arrivalStore option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");
});
